package com.dgut.service.background.service;

import com.dgut.service.background.bean.PassConditions;
import com.dgut.service.background.dao.EnterDao;
import com.dgut.service.background.dao.InspectorDao;
import com.dgut.service.background.dao.OutsDao;
import com.dgut.service.background.dao.UserDao;
import com.dgut.service.background.entity.Enter;
import com.dgut.service.background.entity.Inspector;
import com.dgut.service.background.entity.Outs;
import com.dgut.service.background.entity.User;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExportExcelService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private OutsDao outsDao;

    @Autowired
    private EnterDao enterDao;

    @Autowired
    private InspectorDao inspectorDao;

    public XSSFWorkbook createUserExcel(){
        XSSFWorkbook workbook = new XSSFWorkbook();
        List<User> users = userDao.findAll();
        XSSFSheet sheet = workbook.createSheet("用户表");
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("用户编号");
        row.createCell(1).setCellValue("用户openid");
        row.createCell(2).setCellValue("身份证号");
        row.createCell(3).setCellValue("姓名");
        row.createCell(4).setCellValue("性别");
        row.createCell(5).setCellValue("年龄");
        row.createCell(6).setCellValue("住址");
        row.createCell(7).setCellValue("联系方式");
        int rowNum = 1;
        for (User user:users) {
            row = sheet.createRow(rowNum);
            row.createCell(0).setCellValue(user.getUserId());
            row.createCell(1).setCellValue(user.getOpenid());
            row.createCell(2).setCellValue(user.getIdNumber());
            row.createCell(3).setCellValue(user.getName());
            row.createCell(4).setCellValue(user.getSex());
            row.createCell(5).setCellValue(user.getYear());
            row.createCell(6).setCellValue(user.getRoomId());
            row.createCell(7).setCellValue(user.getPhone());
            rowNum++;
        }
        return workbook;
    }

    //制作出入excel表
    public XSSFWorkbook createPassExcel(String passType, PassConditions passConditions){
        XSSFWorkbook workbook = new XSSFWorkbook();
        String sheetName = null;
        if (passType.equals("outs")){
            sheetName = "进门记录表";
        }else if (passType.equals("enter")){
            sheetName = "出门记录表";
        }
        XSSFSheet sheet = workbook.createSheet(sheetName);
        //设置表头
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("记录编号");
        row.createCell(1).setCellValue("检察员openid");
        row.createCell(2).setCellValue("检察员编号");
        row.createCell(3).setCellValue("检察员姓名");
        row.createCell(4).setCellValue("检察员性别");
        row.createCell(5).setCellValue("检察员电话");
        row.createCell(6).setCellValue("用户openid");
        row.createCell(7).setCellValue("用户编号");
        row.createCell(8).setCellValue("用户姓名");
        row.createCell(9).setCellValue("用户性别");
        row.createCell(10).setCellValue("用户电话");
        row.createCell(11).setCellValue("用户住址");
        row.createCell(12).setCellValue("健康状况");
        row.createCell(13).setCellValue("体温");
        row.createCell(14).setCellValue("录入时间");
        int rowNum = 1;
        //如果为outs，导出符合条件的outs表
        if (passType.equals("outs")){
            //按条件查找outs
            List<Outs> outsList = findOutsByConditions(passConditions);
            //定义一个map储存检察员信息
            Map<String,Inspector> inspectorHashMap = new HashMap<>();
            //定义一个map储存用户信息
            Map<String, User> userMap = new HashMap<>();
            Inspector inspector = new Inspector();
            User user = new User();
            for (Outs outs:outsList) {
                row = sheet.createRow(rowNum);
                row.createCell(0).setCellValue(outs.getOutId());
                row.createCell(1).setCellValue(outs.getInspectorOpenid());
                //判断map中是否存在已有检察员，有则直接在map中取出
                if (inspectorHashMap.containsKey(outs.getInspectorOpenid())){
                    inspector = inspectorHashMap.get(outs.getInspectorOpenid());
                }
                //没有则查找后放入map
                else {
                    inspector = inspectorDao.findInspectorByOpenid(outs.getInspectorOpenid());
                    inspectorHashMap.put(outs.getInspectorOpenid(), inspector);
                }
                //将检察员信息写入excel
                row.createCell(2).setCellValue(inspector.getInspectorId());
                row.createCell(3).setCellValue(inspector.getName());
                row.createCell(4).setCellValue(inspector.getSex());
                row.createCell(5).setCellValue(inspector.getPhone());

                row.createCell(6).setCellValue(outs.getUserOpenid());
                //判断map中是否存在已有检察员，有则直接在map中取出
                if (userMap.containsKey(outs.getUserOpenid())){
                    user = userMap.get(outs.getUserOpenid());
                }
                //没有则查找后放入map
                else {
                    user = userDao.findUserByOpenid(outs.getUserOpenid());
                    if(user == null){
                        user = new User();
                        user.setIdNumber("暂无数据");
                        user.setName("暂无数据");
                        user.setPhone("暂无数据");
                        user.setHealth("暂无数据");
                        user.setRoomId("暂无数据");
                        user.setPhotoPath("暂无数据");
                        user.setSex("暂无数据");
                        user.setYear("暂无数据");
                    }
                    userMap.put(outs.getUserOpenid(), user);
                }
                //将用户信息写入excel
                row.createCell(7).setCellValue(user.getUserId());
                row.createCell(8).setCellValue(user.getName());
                row.createCell(9).setCellValue(user.getSex());
                row.createCell(10).setCellValue(user.getPhone());
                row.createCell(11).setCellValue(user.getRoomId());

                row.createCell(12).setCellValue(outs.getHealth());
                row.createCell(13).setCellValue(outs.getTemp());
                row.createCell(14).setCellValue(outs.getTimes());
                rowNum++;
            }
            //如果为enter，导出符合条件的enter表
        }else if (passType.equals("enter")){
            List<Enter> enterList = findEnterByConditions(passConditions);
            //定义一个map储存检察员信息
            Map<String,Inspector> inspectorHashMap = new HashMap<>();
            //定义一个map储存用户信息
            Map<String, User> userMap = new HashMap<>();
            Inspector inspector = new Inspector();
            User user = new User();
            for (Enter enter:enterList) {
                row = sheet.createRow(rowNum);
                row.createCell(0).setCellValue(enter.getEnterId());
                row.createCell(1).setCellValue(enter.getInspectorOpenid());
                //判断map中是否存在已有检察员，有则直接在map中取出
                if (inspectorHashMap.containsKey(enter.getInspectorOpenid())){
                    inspector = inspectorHashMap.get(enter.getInspectorOpenid());
                }
                //没有则查找后放入map
                else {
                    inspector = inspectorDao.findInspectorByOpenid(enter.getInspectorOpenid());
                    inspectorHashMap.put(enter.getInspectorOpenid(), inspector);
                }
                //将检察员信息写入excel
                row.createCell(2).setCellValue(inspector.getInspectorId());
                row.createCell(3).setCellValue(inspector.getName());
                row.createCell(4).setCellValue(inspector.getSex());
                row.createCell(5).setCellValue(inspector.getPhone());

                row.createCell(6).setCellValue(enter.getUserOpenid());
                //判断map中是否存在已有检察员，有则直接在map中取出
                if (userMap.containsKey(enter.getUserOpenid())){
                    inspector = inspectorHashMap.get(enter.getUserOpenid());
                }
                //没有则查找后放入map
                else {
                    user = userDao.findUserByOpenid(enter.getUserOpenid());
                    if(user == null){
                        user = new User();
                        user.setIdNumber("暂无数据");
                        user.setName("暂无数据");
                        user.setPhone("暂无数据");
                        user.setHealth("暂无数据");
                        user.setRoomId("暂无数据");
                        user.setPhotoPath("暂无数据");
                        user.setSex("暂无数据");
                        user.setYear("暂无数据");
                    }
                    userMap.put(enter.getUserOpenid(), user);
                }
                //将用户信息写入excel
                row.createCell(7).setCellValue(user.getUserId());
                row.createCell(8).setCellValue(user.getName());
                row.createCell(9).setCellValue(user.getSex());
                row.createCell(10).setCellValue(user.getPhone());
                row.createCell(11).setCellValue(user.getRoomId());

                row.createCell(12).setCellValue(enter.getHealth());
                row.createCell(13).setCellValue(enter.getTemp());
                row.createCell(14).setCellValue(enter.getTimes());
                rowNum++;
            }
        }
        return workbook;
    }

    private List<Outs> findOutsByConditions(PassConditions passConditions){
        List<Outs> outs = null;
        String username = passConditions.getUsername();
        String[] towDate = passConditions.getTwoDate();
        int usernameLength = username.length();

        //openid为空、时间为空
        if(usernameLength == 0 && towDate[0].length() == 0){
            outs = outsDao.findAll();
        }
        //openid和时间范围不为空
        else if (usernameLength != 0 && towDate[0].length() != 0){
            outs = outsDao.findAllByTimesBetweenAndUsernameLike(towDate[0], towDate[1], username);
        }
        //openid不为空、时间为空
        else if(usernameLength != 0 && towDate[0].length() == 0){
            outs = outsDao.findAllByUsername(username);
        }
        //openid为空、时间不为空
        else if(usernameLength == 0 && towDate[0].length() != 0){
            outs = outsDao.findAllByTimesBetween(towDate[0], towDate[1]);
        }
        return outs;
    }

    private List<Enter> findEnterByConditions(PassConditions passConditions){
        List<Enter> enters = null;
        String username = passConditions.getUsername();
        String[] towDate = passConditions.getTwoDate();
        int usernameLength = username.length();

        //openid为空、时间为空
        if(usernameLength == 0 && towDate[0].length() == 0){
            enters = enterDao.findAll();
        }
        //openid和时间范围不为空
        else if (usernameLength != 0 && towDate[0].length() != 0){
            enters = enterDao.findAllByTimesBetweenAndUsernameLike(towDate[0], towDate[1], username);
        }
        //openid不为空、时间为空
        else if(usernameLength != 0 && towDate[0].length() == 0){
            enters = enterDao.findAllByUsername(username);
        }
        //openid为空、时间不为空
        else if(usernameLength == 0 && towDate[0].length() != 0){
            enters = enterDao.findAllByTimesBetween(towDate[0], towDate[1]);
        }
        return enters;
    }
}
